package com.hongsheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.BoundSqlInterceptor;
import com.github.pagehelper.PageHelper;
import com.hongsheng.mapper.BillMapper;
import com.hongsheng.pojo.Bill;
import com.hongsheng.pojo.vo.OrderReportVo;
import com.hongsheng.service.BillService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * ClassName:BillServiceImpl
 * Description:
 *
 * @Author: 李媛芝红豆西米露
 * @Create: 2023/8/10 - 18:50
 * @Version: 2023.1.2
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper,Bill> implements BillService {
//    @Autowired
    @Resource
    private BillMapper billMapper;

    @Override
    public List<Bill> selectAll(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
//        List<Bill> all = billMapper.findAll();
        List<Bill> all = billMapper.selectList(null);
        return all;
    }


    @Override
    public Bill findBillById(int id) {
        return billMapper.findBillById(id);
    }

    @Override
    public List<Bill> findBillByType(String type) {
        return billMapper.findBillByType(type);
    }

    @Override
    public List<Bill> findByAmount(int amount1, int amount2) {
        return billMapper.findByAmount(amount1,amount2);
    }

    @Override
    public boolean insertBill(Bill bill) {
        return billMapper.insertBill(bill)>0;
    }

    @Override
    public boolean updateBill(Bill bill) {
        return billMapper.updateBill(bill)>0;
    }

    @Override
    public boolean deleteBill(int id) {
        return billMapper.deleteBill(id)>0;
    }

//    @Override
//    public IPage<Bill> getPage(int currentPage, int size, Bill bill) {
//        return null;
//    }

    @Override
    public IPage<Bill> getPage(int currentPage, int size, Bill bill,int minAmount,int maxAmount) {
        //按条件查询
        LambdaQueryWrapper<Bill> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(bill.getType()),Bill::getType,bill.getType());
        if (bill.getCreateTime() != null) {
            wrapper.like(Bill::getCreateTime, new SimpleDateFormat("yyyy-MM-dd").format(bill.getCreateTime()));
        }
        if(maxAmount==0 || maxAmount<minAmount){
wrapper.gt(Bill::getAmount,minAmount);
        }else {
            wrapper.between(Bill::getAmount,minAmount,maxAmount);
        }

/* QueryWrapper<Book> wrapper=new QueryWrapper<>();
          wrapper.like("type",book.getType()).like("name",book.getName()).like("description",book.getDescription());*/
        Page<Bill> page=new Page<>(currentPage,size);
        Page<Bill> page1 = baseMapper.selectPage(page, wrapper);
        return page1;
    }

//    @Override
//    public IPage<Bill> getPage(int currentPage, int size, Bill bill,int minAmount,int maxAmount) {
//        //按条件查询
//        LambdaQueryWrapper<Bill> wrapper=new LambdaQueryWrapper<>();
//        wrapper.like(Strings.isNotEmpty(bill.getType()),Bill::getType,bill.getType());
//        if (bill.getCreateTime() != null) {
//            wrapper.like(Bill::getCreateTime, new SimpleDateFormat("yyyy-MM-dd").format(bill.getCreateTime()));
//        }
//        if(maxAmount==0 || maxAmount<minAmount){
//wrapper.gt(Bill::getAmount,minAmount);
//        }else {
//            wrapper.between(Bill::getAmount,minAmount,maxAmount);
//        }
//        Page<Bill> page=new Page<>(currentPage,size);
//        Page<Bill> page1 = baseMapper.selectPage(page, wrapper);
//        return page1;
//    }

    /**
     * 查询每月收入
     * @return
     */
    @Override
    public List<OrderReportVo> selectBillByMonth() {
        return billMapper.selectBillByMonth();
    }

    /**
     * 查询账单每月支出
     * @return
     */
    @Override
    public List<OrderReportVo> selectBillByMoney() {
        return billMapper.selectBillByMoney();
    }

}
